<template>
  <div>
    <!-- dialog对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="addShow"
      width="400px"
      class="dialog"
      @close="$emit('close')"
    >
      <!-- 内容主体区 -->
      <el-form
        :model="BodyInfo"
        :rules="rules"
        ref="addForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item
          label="所属学科"
          prop="subjectID"
          v-if="!this.$route.query.id"
        >
          <el-select
            v-model="BodyInfo.subjectID"
            placeholder="请选择"
            class="select"
          >
            <el-option
              v-for="(item, i) in formModel"
              :key="i"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input
            v-model="BodyInfo.directoryName"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部按钮区 -->
      <span slot="footer" class="dialog-footer ">
        <el-button type="primary" @click="addForm()" class="bttn-button"
          >确 定</el-button
        >
        <el-button @click="close()" class="btn-button">取 消</el-button>
      </span>
    </el-dialog>
    <!-- dialog对话框 /-->
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects'
import { add, update } from '@/api/hmmm/directorys'
export default {
  name: 'DirectorysAdd',
  props: {
    item: {
      type: Object,
      redirect: true
    },
    title: {
      type: String,
      redirect: true
    }
  },
  data() {
    return {
      formModel: [], // 数据列表
      BodyInfo: {
        id: null,
        subjectID: null, // 学科ID
        directoryName: null // 目录名称
      },
      //  表单验证
      rules: {
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ],
        subjectID: [
          { required: true, message: '请选所属学科', trigger: 'blur' }
        ]
      },
      // 控制用户的显示和隐藏
      addShow: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 弹窗
    open() {
      this.addShow = true
    },
    close() {
      this.$emit('clear')
      this.addShow = false
    },
    // 获取学科目录
    async getList() {
      const { data: res } = await simple()
      // console.log(res, 11)
      this.formModel = res
    },
    // 确定 验证
    addForm() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          if (this.$route.query.id) {
            this.BodyInfo.subjectID = Number(this.$route.query.id)
          }
          // 可以发起正真的请求
          if (this.BodyInfo.id) {
            await update(this.BodyInfo)
          } else {
            await add(this.BodyInfo)
          }
          this.close()
          this.$message.success(`${this.title}成功！`)
          // 把值给父组件穿过去
          this.$emit('newDataes')
        } else {
          this.$message.error('*号为必填项!')
        }
      })
    }
    // 监听添加用户对话框的关闭事件
    /* addVueClose() {
      // this.$refs.addForm.resetFields()
    } */
  },
  watch: {
    item: {
      handler() {
        this.BodyInfo.id = this.item.id
        this.BodyInfo.subjectID = this.item.subjectID
        this.BodyInfo.directoryName = this.item.directoryName
      }
    }
  }
}
</script>

<style scoped>
.select {
  width: 260px;
}
.btn-button {
  position: absolute;
  bottom: 20px;
  right: 150px;
}
.bttn-button {
  position: absolute;
  bottom: 20px;
  right: 60px;
}
</style>
